package com.cn.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cn.bean.Resident;
import com.cn.service.ResidentService;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class ResidentAction extends ActionSupport {
	private Resident resident;
	private ResidentService residentService = null;
	HttpServletRequest request;
	HttpServletResponse response;

	public ResidentService getResidentService() {
		return residentService;
	}

	public void setResidentService(ResidentService residentService) {
		this.residentService = residentService;
	}

	public Resident getResident() {
		return resident;
	}

	public void setResident(Resident resident) {
		this.resident = resident;
	}

	/**
	 * 安卓端登录,成功则返回该对象
	 * 失败则返回failed
	 */
	public void login() {
		try {
			List list = residentService.login();
			if(!list.isEmpty()){
				response = ServletActionContext.getResponse();
				response.setContentType("text/json; charset=UTF-8");
				Gson gson = new Gson();
				String json = gson.toJson(list);
				PrintWriter out = response.getWriter();
				out.print(json);
			}
			else{
				response = ServletActionContext.getResponse();
				response.setContentType("text/html;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print("failed");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 居民手机号注册
	public void register() {
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = residentService.register();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 完善个人信息
	public void editInfor() {
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			String result = residentService.editInfor();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 安卓端获取个人信息
	public void getInfor() {
		try {
			List list = residentService.getInfor();
			Gson gson = new Gson();
			String json = gson.toJson(list);
			response = ServletActionContext.getResponse();
			response.setContentType("text/json; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//用户收藏供应商
	public void collectProvider()
	{
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = residentService.collect();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//用户添加建议反馈
	public void advice()
	{
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = residentService.advice();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
